package cn.pzhu.handler;

import cn.pzhu.exception.MyException;
import com.jfinal.handler.Handler;
import com.jfinal.kit.PropKit;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * Created by dongp on 2016/4/22.
 */
public class GlobalHandler extends Handler {

    private static Logger log = Logger.getLogger(GlobalHandler.class);

    @Override
    public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {

        log.debug("----------------设置全局编码格式--------------");
        String encode = PropKit.get("encode");
        try {
            request.setCharacterEncoding(encode);
        } catch (UnsupportedEncodingException e) {
           throw new MyException("设置请求编码格式异常");
        }
        response.setCharacterEncoding(encode);
        //设定浏览器以那种类型来解析
        response.setContentType("text/html;charset=" + encode);
        request.setAttribute("basePath",request.getContextPath());
        next.handle(target, request, response, isHandled);

    }
}
